<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MentController extends Controller
{
	public function getAdd()
	{
		return view('admin.adver.add');
	}


	public function postInsert(Request $request)
	{
		$data['aname'] = $request->input('aname');
		$data['atime'] = $request->input('atime');
		$data['aetime'] = $request->input('aetime');
		$data['aprice'] = $request->input('aprice');
		$data['atext'] = $request->input('atext');
		// $data['atext'] = $_FILES['atext']['name'];
		// dd($data['atext']);
		// $res = DB::table('advertisement')->insert($data);


		// dd($res);
		if($_FILES['atext']['name']){

                //随机文件名
                $name = time()+rand(1,9999999);//名
                // dd($name);
                //获取后缀名
                $su = $request->file('atext')->getClientOriginalExtension();//png
                // dd($su);
                $arr = array('jpg','png','jpeg','tiff');
                //判断
                if(!in_array($su,$arr)){
                    echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://mao.cn/test/add";
                    </script>';
                }
            //将文件移动到指定目录下
            $request->file('atext')->move('./uploads/wzlog/',$name.'.'.$su);

            $a = '/uploads/wzlog/'.$name.'.'.$su;
            // dd($a);
        }

            $data['atext'] = $a;

		$res = DB::table('advertisement')->insert($data);
			// dd($res);
            // $result = DB::table('advertise	ment')->update($data);
            // return back();  


		// 判断
		if($res)
		{
			return redirect('/admin/adver/list')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}

	public function getList()
	{
		$res = DB::table('advertisement')->select()->get();
		// dd($res);
		// dd($res->atext);
		// dd($res[0]->atext);
		return view('admin.adver.list',['res'=>$res]);
	}

	// 删除

	 public  function postDelete(Request $request){
 // dd($request -> input('id'));
	$id = $request -> input('id');

	$res = DB::table('advertisement')->where('id',$id)->delete();
	if($res){
    echo 1;
	}else{
		echo 2;
	}
 }


 	// 修改

 	// 修改显示

 	public function getEdit($id)
 	{
	     //根据用户id查询数据
		$adver = DB::table('advertisement')->where('id',$id)->first();

		// dd($adver);

		//解析模板
		return view('admin.adver.edit',['adver'=>$adver]); 		
    }

    // 修改
	public function postUpdate(Request $request)
	{

		// dd($request->all());
		// 获取id
		$id = $request->only(['id']);

		//数据验证
    		$this->validate($request, [

        'atext' => 'regex:[1\d{10}]',

    ],[
         'atext.regex'=>'收费金额格式不正确',
    ]);

		// dd($id);
		$aa = $request['atext'] -> getClientOriginalName();

		$data = '/uploads/wzlog/'.$aa;
		// dd($data);
		// dd($data['atext']);
		$data = $request->only(['aname','atime','aetime','aprice']);
		// dd($data);
		// 执行
		$res = DB::table('advertisement')->where('id',$id)->update($data);
		// dd($res);
	// dd($res);

		// 获取id
		$id = $request->only(['id']);	
		if($_FILES['atext']['name']){
                //随机文件名
                $name = time()+rand(1,9999999);//名
                // dd($name);
                //获取后缀名
                $su = $request->file('atext')->getClientOriginalExtension();//png
                // dd($su);
                $arr = array('jpg','png','jpeg','tiff');
                //判断
                if(!in_array($su,$arr)){
                    echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://mao.cn/test/add";
                    </script>';
                }
	            //将文件移动到指定目录下
	            $request->file('atext')->move('./uploads/wzlog/',$name.'.'.$su);
	            $a = '/uploads/wzlog/'.$name.'.'.$su;
	            $data = $request->only(['aname','atime','aetime','aprice']);
				$data['atext'] = $a;
				// 执行
				$res = DB::table('advertisement')->where('id',$id)->update($data);
        }else{
        	$data = $request->only(['aname','atime','aetime','aprice']);
			// 执行
			$res = DB::table('advertisement')->where('id',$id)->update($data);
        }

		// 判断
		if($res)
		{
			return redirect('/admin/adver/list')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}


}
